package com.hsc.yalebao.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.hsc.pcddfivepcdd.R;
import com.hsc.yalebao.db.DatabaseHelper;
import com.hsc.yalebao.http.AppConstants;
import com.hsc.yalebao.http.RequestNet;
import com.hsc.yalebao.model.IsBoundPhoneBean;
import com.hsc.yalebao.model.TokenModel;
import com.hsc.yalebao.newui.UpNewactivity;
import com.hsc.yalebao.tools.FileTools;
import com.hsc.yalebao.tools.SystemHelper;
import com.hsc.yalebao.tools.UiUtil;
import com.hsc.yalebao.weight.LogUtils;
import com.hsc.yalebao.weight.MyDialog1;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

public class SplashActivity extends BaseActivity {
    private String TAG = "SplashActivity";
    private Context context;
    boolean isFirstIn = false;
    private ImageView iv_bg;
    private ImageView iv_logo;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramAnonymousMessage) {
            switch (paramAnonymousMessage.what) {
                case 1000:
                    goHome();
                    break;
                case 1001:
                    goHome();
                    break;
            }
        }
    };
    private MyDialog1 myDialog12;

    private void GetCheckState() {
        HashMap localHashMap = new HashMap();
        String str = getPackageName();
        localHashMap.put("type", str);
        localHashMap.put("version", getAppVersionCode(this.context));
        localHashMap.put("wapversion", getAppVersionCode(this.context));
        LogUtils.d(this.TAG, "URL:" + AppConstants.URL_GETCHECKSTATE);
        RequestNet.get(this.context, AppConstants.URL_GETCHECKSTATE, localHashMap, new StringCallback() {
            public void onError(Call paramAnonymousCall, Exception paramAnonymousException) {
                LogUtils.e(SplashActivity.this.TAG, "获取失败,GetCheckState-result:" + paramAnonymousException);
                SplashActivity.this.serverDialog();
            }

            public void onResponse(String paramAnonymousString) {
                LogUtils.d(SplashActivity.this.TAG, "===result:" + paramAnonymousString);
                if (UiUtil.IsValueEmpty(paramAnonymousString)) {
                    LogUtils.d(SplashActivity.this.TAG, "为空时result:" + paramAnonymousString);
                    SplashActivity.this.serverDialog();
                    return;
                }
                IsBoundPhoneBean localIsBoundPhoneBean;
                int i;
                do {
                    do {
                        Gson localGson = new Gson();
                        try {
                            localIsBoundPhoneBean = (IsBoundPhoneBean) localGson.fromJson(paramAnonymousString, IsBoundPhoneBean.class);
                            if (localIsBoundPhoneBean == null) {
                                LogUtils.e(SplashActivity.this.TAG, "isBoundPhoneBean，获取失败");
                                SplashActivity.this.serverDialog();
                                return;
                            }
                        } catch (Exception localException) {
                            while (true) {
                                localException.printStackTrace();
                                localIsBoundPhoneBean = null;
                            }
                        }
                    }
                    while (localIsBoundPhoneBean.getFlag() != 1);
                    i = localIsBoundPhoneBean.getState();
                    if (i == 1) {
                        SplashActivity.this.startActivity(new Intent(SplashActivity.this, UpNewactivity.class));
                        CustomApplication.app.finishActivity(SplashActivity.this);
                        return;
                    }
                    if (i == 0) {
                        SplashActivity.this.startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        CustomApplication.app.finishActivity(SplashActivity.this);
                        return;
                    }
                }
                while (i != 2);
                Intent localIntent = new Intent(SplashActivity.this, HtmlActivity.class);
                localIntent.putExtra("url", localIsBoundPhoneBean.getUrl());
                localIntent.putExtra("type", -1);
                SplashActivity.this.startActivity(localIntent);
                CustomApplication.app.finishActivity(SplashActivity.this);
            }
        });
    }

    public static String getAppVersionCode(Context paramContext) {
        try {
            String str = paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0).versionName;
            return str;
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            localNameNotFoundException.printStackTrace();
        }
        return "";
    }

    public static void getNetIp() {
        new Thread(new Runnable() {
            // ERROR //
            public void run() {

            }
        }).start();
    }

    private void getServer() {
        SystemHelper.getServer(this);
        getToken();
    }

    private void getToken() {
        HashMap localHashMap = new HashMap();
        localHashMap.put("grant_type", "client_credentials");
        RequestNet.postToken(this.context, AppConstants.URL_GETTOKEN, localHashMap, new StringCallback() {
            public void onError(Call paramAnonymousCall, Exception paramAnonymousException) {
                LogUtils.e(SplashActivity.this.TAG, "获取失败,getParent2RoomListInfo-result:" + paramAnonymousException);
            }

            public void onResponse(String paramAnonymousString) {
                SplashActivity.this.dismissLoadingDialog();
                Gson localGson = new Gson();
                TokenModel localTokenModel = null;
                try {
                    localTokenModel = (TokenModel) localGson.fromJson(paramAnonymousString, TokenModel.class);
                    if (localTokenModel == null) {
                        LogUtils.e(SplashActivity.this.TAG, "获取失败");
                        return;
                    }
                } catch (Exception localException) {

                    do {
                        AppConstants.NO_LOGIN_TOKEN = localTokenModel.getAccess_token();
                        while (true) {
                            localException.printStackTrace();
                            localTokenModel = null;
                        }

                    } while (localTokenModel == null);
                }
            }
        });
    }

    private void goHome() {
        GetCheckState();
    }

    private void initDataAndStartMain() {
        this.isFirstIn = getSharedPreferences("first_pref", 0).getBoolean("isFirstIn", true);
        FileTools.copyDb("yalebao.db");
        new DatabaseHelper(this);
        if (!this.isFirstIn) {
            this.mHandler.sendEmptyMessageDelayed(1000, 1500L);
            return;
        }
        this.mHandler.sendEmptyMessageDelayed(1001, 1500L);
    }

    private void serverDialog() {
        if (this.myDialog12 == null) {
            this.myDialog12 = new MyDialog1(this);
            this.myDialog12.setCancelable(false);
            this.myDialog12.setMessage("服务器连接失败，请重新连接");
            this.myDialog12.btn_confirm.setBackgroundResource(R.drawable.conglian_bg_selector);
            this.myDialog12.btn_confirm.setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    SystemHelper.getServer(SplashActivity.this);
                    SplashActivity.this.getToken();
                    SplashActivity.this.GetCheckState();
                    SplashActivity.this.myDialog12.dismiss();
                }
            });
        }
        this.myDialog12.show();
    }


    protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        if (paramInt2 == 0) {
            if (-1 != SystemHelper.getNetworkType(this))
                initDataAndStartMain();
        } else
            return;
        LogUtils.d(this.TAG, "网络连接仍不可用");
    }

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.splash);
        this.context = this;
        getServer();
        CustomApplication.app.addActivity(this);
        this.iv_bg = ((ImageView) findViewById(R.id.iv_bg));
        this.iv_logo = ((ImageView) findViewById(R.id.iv_logo));
        LinearLayout.LayoutParams localLayoutParams = (LinearLayout.LayoutParams) this.iv_logo.getLayoutParams();
        localLayoutParams.width = (7 * (CustomApplication.app.displayMetrics.widthPixels / 10));
        localLayoutParams.height = localLayoutParams.width;
        this.iv_logo.setLayoutParams(localLayoutParams);
        new Timer().schedule(new TimerTask() {
                                 public void run() {
                                     SplashActivity.this.initDataAndStartMain();
                                 }
                             }
                , 1L);
    }

}